package javabasics.task2;

import java.util.Scanner;
import java.util.ArrayList;
import java.lang.Math;

public class Neighbours {

	public static void main(String[] args) {
	  String[] inputStrings = readInputIntoArrayOfString();

	  ArrayList<Integer> intList = convertInputIntoArrayListOfIntegers(inputStrings);

	  System.out.print("The index of the first number with the smallest neighbouring distance: "
				+ firstIndex(intList));
	}

	private static ArrayList<Integer> convertInputIntoArrayListOfIntegers(
			String[] inputStrings) {
		ArrayList<Integer> intList = new ArrayList<Integer>();
		  for(String s: inputStrings){
			  intList.add(Integer.parseInt(s));
		  }
		return intList;
	}

	private static String[] readInputIntoArrayOfString() {
		Scanner input = new Scanner(System.in);
		  System.out.println("Input your numbers with whitespace between:");
		  String[] inputStrings = input.nextLine().split(" ");
		  while (inputStrings.length==1){
			  System.out.println("Invalid Input. Input your numbers with whitespace between one more time:");
			  input = new Scanner(System.in);
			  inputStrings = input.nextLine().split(" ");
		  }
		  input.close();
		return inputStrings;
	}

	private static int firstIndex(ArrayList<Integer> temp) {
		int distance = Math.abs(temp.get(0) - temp.get(1));
		int index = 0;

		for (int i = 1; i < temp.size() - 1; i++) {
			if (Math.abs(temp.get(i) - temp.get(i + 1)) < distance) {
				distance = Math.abs(temp.get(i) - temp.get(i + 1));
				index = i;
			}
		}
		return index;
	}
}